/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.controller;

import com.mavenrentcars.exception.ViewException;
import com.mavenrentcars.model.Car;
import com.mavenrentcars.model.Place;
import com.mavenrentcars.service.AccountService;
import com.mavenrentcars.service.CarService;
import com.mavenrentcars.service.PlaceService;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author serg
 */
@Controller
public class SearchController {

    @Autowired
    AccountService accountService;
    @Autowired
    CarService carService;
    @Autowired
    PlaceService placeService;

    @RequestMapping(value = "catalog_view.htm", method = RequestMethod.GET)
    public ModelAndView view(
            HttpServletResponse httpServletResponse) {
        ModelAndView mav = new ModelAndView("catalog/list");
        try {
            List<Place> places = placeService.getAll();
            mav.addObject("list", places);
            mav.addObject("acc", accountService);
        } catch (ViewException ex) {
            Logger.getLogger(SearchController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return mav;
    }

    private List<Map<String, String>> format(List<Car> list) {
        List<Map<String, String>> res = new ArrayList<Map<String, String>>();
        for (Car item : list) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("id", Integer.toString(item.getId()));
            map.put("model", item.getModel());
            map.put("output_year", Integer.toString(item.getOutputYear()));
            map.put("price", item.getPrice().toString());
            map.put("insurance", item.getInsurance());
            map.put("start_place", item.getStartPlace().getPlaceName());
            res.add(map);
        }
        return res;
    }

    @RequestMapping(value = "cars_list.htm", method = RequestMethod.POST)
    public ModelAndView list(
            HttpServletResponse httpServletResponse,
            @RequestParam(value = "start_place", required = false, defaultValue = "") String place,
            @RequestParam(value = "start_date", required = false, defaultValue = "") String start,
            @RequestParam(value = "end_date", required = false, defaultValue = "") String end) {

        ModelAndView mav = new ModelAndView("jsonView");

        try {

            if (place.equals("") && (start.equals("") || end.equals(""))) {
                List<Car> list = carService.getAll();
                mav.addObject("list", (list != null) ? format(list) : "");
            }

            if (!place.equals("") && (start.equals("") || end.equals(""))) {
                Place start_place = placeService.get(Integer.parseInt(place));
                List<Car> list = carService.getAll(start_place);
                mav.addObject("list", (list != null) ? format(list) : "");
            }

            if ((!start.equals("") && !end.equals("")) && place.equals("")) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                List<Car> list = carService.getAll(df.parse(start), df.parse(end));
                mav.addObject("list", (list != null) ? format(list) : "");
            }

            if (!place.equals("") && !start.equals("") && !end.equals("")) {
                Place start_place = placeService.get(Integer.parseInt(place));
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                List<Car> list = carService.getAll(
                        start_place,
                        df.parse(start),
                        df.parse(end));
                mav.addObject("list", (list != null) ? format(list) : "");
            }

            mav.addObject("res", true);
        } catch (Exception ex) {
            mav.addObject("error", ex.getMessage());
            mav.addObject("res", false);
        }
        return mav;
    }
}
